﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Loginaspx : System.Web.UI.Page
{
   // User userSrv = new User();
    MyDataDataContext db = new MyDataDataContext();
    

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            //NewUser.aspx页面传递过来的查询字符串变量name值非空
            if(Request.QueryString["name"]!=null)
            {
                txtName.Text = Request.QueryString["name"];
                lblMsg.Text = "注册成功，请登录！";
            }
        }
    }
    protected void BtnLogin_Click(object sender, EventArgs e)
    {
        User user = (from c in db.User
                    where c.UserNickname == txtName.Text && c.UserPwd == txtPwd.Text
                    select c).FirstOrDefault();

        Manager manager = (from c in db.Manager
                     where c.ManagerName == txtName.Text && c.ManagerPwd== txtPwd.Text
                     select c).FirstOrDefault();
        
        if (user!=null)//一般用户登录
        {
            if (user.blockflag == "1")
            {
                lblMsg.Text = "您的账户已被封禁！";
            }
            else
            {
                Session.Clear();
                Session["UserId"] = user.UserID;
                Session["UserName"] = txtName.Text;
                Response.Redirect("~/Default.aspx");
            }
        }
        else if (manager != null)//管理员登录
        {
            Session.Clear();
            Session["AdminId"] = manager.ManagerID;
            Session["AdminName"] = txtName.Text;
            Response.Redirect("~/admin/Administrator.aspx");
        }
        else
        {
            lblMsg.Text = "用户名或密码错误！";
        }
    }
}